/**
 * Project Name:YCApiServer-Api
 * File Name:InspireService.java
 * Package Name:com.yc.music.service.service103
 * Date:2016年5月12日上午11:31:51
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service103;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.yc.music.mapper.dao.InspireMapper;
import com.yc.music.model.inspire.Inspire;
import com.yc.music.common.context.YcContext;
import com.yc.music.common.util.BindingResultUtil;
import com.yc.music.common.util.QiNiuUpload;
import com.yc.music.common.util.RoomUtil;
import com.yc.music.context.YtConstant;

/**
 * 灵感记录 service
 * ClassName: InspireService <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年5月12日 上午11:31:51 <br/>
 *
 * @author panguixiang
 * @version 
 */
@Service
public class InspireService {

	
	@Autowired
	private InspireMapper InspireMapper;
	
	@Value("${audio_buckname}")
	private String audio_buckname;
	
	@Value("${pic_buckname}")
	private String pic_buckname;
	
	@Value("${Pic_QiLiu}")
	private String pIC_QiLiu;
	
	@Value("${Audio_QiLiu}")
	private String Audio_QiLiu;
	
	/**
	 * 
	 * save:(新增灵感记录). <br/> 
	 *
	 * @author panguixiang
	 * @param spire
	 * @param binding
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> saveSprie(Inspire spire, BindingResult binding, Map<String,Object> result) throws Exception{
		
		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if(StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		if(StringUtils.isBlank(spire.getAudio()) && StringUtils.isBlank(spire.getPics())
				&& StringUtils.isBlank(spire.getSpirecontent())) {//文字，声音，图片，必须至少有一个不为空
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.INSPIRE_PAREM_ERROR);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		spire.setPics(RoomUtil.batchSlash(spire.getPics()));
		spire.setAudio(RoomUtil.batchSlash(spire.getAudio()));
		InspireMapper.save(spire);
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, YtConstant.SYS_SUCCSS_MSG);
		return result;
	}
	
	/**
	 * 
	 * updateSprie:(修改灵感记录). <br/> 
	 *
	 * @author panguixiang
	 * @param spire
	 * @param binding
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> updateSprie(Inspire spire, BindingResult binding, Map<String,Object> result) throws Exception{
		
		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if(StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		if(spire.getItemid()==0 || spire.getItemid()==0l){
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.ID_NULL);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		if(StringUtils.isBlank(spire.getAudio()) && StringUtils.isBlank(spire.getPics())
				&& StringUtils.isBlank(spire.getSpirecontent())) {//文字，声音，图片，必须至少有一个不为空
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.INSPIRE_PAREM_ERROR);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		spire.setPics(RoomUtil.batchSlash(spire.getPics()));
		spire.setAudio(RoomUtil.batchSlash(spire.getAudio()));
		InspireMapper.updateInsprie(spire);
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, YtConstant.SYS_SUCCSS_MSG);
		return result;
	}
	
	/**
	 * 
	 * deleteSprie:(删除灵感记录). <br/> 
	 *
	 * @author panguixiang
	 * @param id
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> deleteSprie(Long id, Map<String,Object> result) throws Exception{
		Inspire inspire = InspireMapper.getInspireByid(id);
		if(inspire!=null){
			InspireMapper.deleteInspireByid(id);
			deleteInpsiQiNiuFile(inspire,pic_buckname,audio_buckname);
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, YtConstant.SYS_SUCCSS_MSG);
		return result;
	}
	
	/**
	 * 
	 * getSprie:(获得明细). <br/> 
	 *
	 * @author panguixiang
	 * @param id
	 * @param result
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Map<String,Object> getSprie(Long id, Map<String,Object> result) throws Exception{
		Inspire inspire = InspireMapper.getInspireFullByid(id);
		if(inspire!=null) {
			inspire.setAudiodomain(Audio_QiLiu);
			inspire.setPicdomain(pIC_QiLiu);
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, inspire);
		return result;
	}
	
	/**
	 * 
	 * deleteInpsiQiNiuFile:(删除七牛上的文件). <br/> 
	 *
	 * @author panguixiang
	 * @param inspire
	 * @param pIC_QiLiu
	 * @param Audio_QiLiu
	 * @throws Exception
	 * @since JDK 1.7
	 */
	private static void deleteInpsiQiNiuFile(final Inspire inspire,final String pIC_QiLiu,
			final String Audio_QiLiu) throws Exception{
		
		new Thread(){
			public void run(){
				String pics=inspire.getPics();
				if(StringUtils.isNotBlank(pics)) {
					String[] picArr = pics.split(",");
					for(String pic:picArr) {
						if(pic.charAt(0)==YcContext.Slashchar){
							pic=pic.replaceFirst(YcContext.Slash, YcContext.EMPTY_STRING);
						}
						QiNiuUpload.deleteByKey(pic, pIC_QiLiu);
					}
				}
				if(StringUtils.isNotBlank(inspire.getAudio())) {
					String autdio = inspire.getAudio();
					if(autdio.charAt(0)==YcContext.Slashchar){
						autdio=autdio.replaceFirst(YcContext.Slash, YcContext.EMPTY_STRING);
					}
					QiNiuUpload.deleteByKey(autdio, Audio_QiLiu);
				}
			}
		}.start();
	}

}
